package com.unhz.taigu.controller.permission;

import com.unhz.generator.pojo.SysPermission;
import com.unhz.generator.pojo.SysRolePermission;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import com.unhz.taigu.service.SysPermissionService;
import com.unhz.taigu.service.rolepermission.RolePermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by cxg on 2017/11/13 0013.
 *
 * @Author 曹显贵
 * @Date 2017/11/13 0013 13:44
 */
@Controller
@RequestMapping("/permissionController")
public class SysPermissionController {
    static final int ROLE_ID = 1;
    private final Logger logger = LoggerFactory.getLogger(SysPermissionController.class);

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @RequestMapping("/findAllPermission")
    @RequiresPermissions("permission:findAll")
    public String findAllPermission(Model model,
                                    @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                    @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords){
        //List<Permission> permissions = permissionService.selectAllPermission();
        Page<SysPermission> page = sysPermissionService.selectPagePermission(pageNo, PageSize.pageSize,keywords);
        model.addAttribute("page",page);
        model.addAttribute("keywords",keywords);
        return "system/quanxian/quanxian-list.jsp";
    }

    //权限接口
    @RequestMapping("/iFindAllPermission")
    @ResponseBody
    public List<SysPermission> iFindAllPermission(HttpServletResponse response){
        response.setContentType("text/JavaScript; charset=utf-8");
        List<SysPermission> list = sysPermissionService.selectAllPermission();
        /*for(Permission permission:list){
            if("menu".equals(permission.getPermissionType())){
                permission.setPermissionType("总后台");
            }else if("menus".equals(permission.getPermissionType())){
                permission.setPermissionType("菜单栏");
            }else if("mainMenu".equals(permission.getPermissionType())){
                permission.setPermissionType("子菜单");
            }else if("permission".equals(permission.getPermissionType())){
                permission.setPermissionType("菜单功能");
            }
        }*/
        return list;
    }

    //跳转到修改页面
    @RequestMapping("/itoupdate")
    public String itoupdate(@RequestParam("permissionId")Integer permissionId,
                            Model model){

        SysPermission sysPermission = sysPermissionService.selectByPermissionId(permissionId);
        List<SysPermission> sysPermissions = null;
        if(sysPermission.getPermissionType().equals("mainMenu")){
            sysPermissions = sysPermissionService.findAllParent();
        }else if(sysPermission.getPermissionType().equals("permission")){
            sysPermissions = sysPermissionService.findAllChild();
        }else if(sysPermission.getPermissionType().equals("menus")){
            sysPermissions = sysPermissionService.findAllBig();
        }
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("per", sysPermission);
        return "bite/system/quanxian/quanxian-update.jsp";
    }
    //跳转到添加父级页面
    @RequestMapping("/itoAdd")
    public String itoAdd(Model model){

        List<SysPermission> sysPermissions = sysPermissionService.findAllParent();
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("permissionMenu","parent");

        return "bite/system/quanxian/quanxian-addOrder.jsp";
    }
    //跳转到添加子级页面
    @RequestMapping("/itoAddChild")
    public String itoAddChild(Model model, HttpServletRequest request){
        request.getSession().setAttribute("child","childPermission");
        List<SysPermission> sysPermissions = sysPermissionService.findAllChild();
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("permissionMenu","child");

        return "bite/system/quanxian/quanxian-addOrder.jsp";
    }

    //添加权限
    @RequestMapping("/toAdd")
    public String toAdd(Model model)
    {
        //查询顶级分类
        List<SysPermission> sysPermissions = sysPermissionService.findAllParent();
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("permissionMenu","parent");
        return "system/quanxian/quanxian-add.jsp";
    }

    //执行添加
    @RequestMapping("/insert")
    public String insert(SysPermission sysPermission, Model model){
        String index = sysPermissionService.insertPermission(sysPermission);
        //默认添加admin总管理员的权限
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setPermissionId(sysPermission.getId());
        sysRolePermission.setRoleId(ROLE_ID);
        int partIndex = rolePermissionService.insertRolePermission(sysRolePermission);
        if(partIndex > 0){
            logger.info("添加管理员的权限成功");
        }
        return "redirect:findAllPermission";
    }
    //跳转到修改页面
    @RequestMapping("/toUpdate")
    @RequiresPermissions("permission:update")
    public String toUpdate(@RequestParam("permissionId")Integer permissionId,Model model){
        //查询
        SysPermission sysPermission = sysPermissionService.selectByPermissionId(permissionId);
        List<SysPermission> sysPermissions = null;
        if(sysPermission.getPermissionType().equals("mainMenu")){
            sysPermissions = sysPermissionService.findAllParent();
        }else if(sysPermission.getPermissionType().equals("permission")){
            sysPermissions = sysPermissionService.findAllChild();
        }else if(sysPermission.getPermissionType().equals("menus")){
            sysPermissions = sysPermissionService.findAllBig();
        }
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("per", sysPermission);
        return "system/quanxian/quanxian-update.jsp";
    }
    //执行修改
    @RequestMapping("/update")
    @RequiresPermissions("permission:update")
    public String update(SysPermission sysPermission){
        //查询
        String index = sysPermissionService.updatePermission(sysPermission);

        return "redirect:findAllPermission";
    }

    //添加子级
    @RequestMapping("/toAddChild")
    @RequiresPermissions("permission:add")
    public String toAddChild(Model model)
    {
        //查询顶级分类
        List<SysPermission> sysPermissions = sysPermissionService.findAllChild();
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("permissionMenu","child");
        return "system/quanxian/quanxian-add.jsp";
    }
    //删除权限
    @RequestMapping("/delete")
    @RequiresPermissions("permission:delete")
    @ResponseBody
    public JsonResult delete(@RequestParam("permissionId")Integer permissionId){

        //删除权限,先删除角色权限表
        String result = rolePermissionService.deleteByPermissionId(permissionId);
        //删除权限
        int code = sysPermissionService.deleteByPermissionId(permissionId);
        if(code > 0){
            return JsonResult.success("删除成功");
        }
        return JsonResult.failure("删除失败");
    }

    //跳转到详情页面
    @RequestMapping("/toDetail")
    public String detail(@RequestParam("permissionId")Integer permissionId,Model model){
        //查询
        SysPermission sysPermission = sysPermissionService.selectByPermissionId(permissionId);
        List<SysPermission> sysPermissions = null;
        if(sysPermission.getPermissionType().equals("mainMenu")){
            sysPermissions = sysPermissionService.findAllParent();
        }else if(sysPermission.getPermissionType().equals("permission")){
            sysPermissions = sysPermissionService.findAllChild();
        }else if(sysPermission.getPermissionType().equals("menus")){
            sysPermissions = sysPermissionService.findAllBig();
        }
        model.addAttribute("permissionList", sysPermissions);
        model.addAttribute("permission", sysPermission);
        return "system/quanxian/quanxian-detail.jsp";
    }
}
